package com.yxhl.zoume.core.tripsmgmt.ui.adapter;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yxhl.protobuf.Contract;
import com.yxhl.protobuf.ContractType;
import com.yxhl.protobuf.OrderBizType;
import com.yxhl.protobuf.OrderPayStatus;
import com.yxhl.zoume.R;
import com.yxhl.zoume.core.tripsmgmt.ui.fragment.dialog.CancelTicketDialogFragment;
import com.yxhl.zoume.utils.StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by sugarya on 16/3/30.
 * 行程详情页ListView适配器
 */
public class PassengerDetailAdapter extends BaseAdapter {

    private static final String TAG = "PassengerDetailAdapter";

    private List<Contract> mPassengerList;
    private FragmentManager mFragmentManager;
    private Context mContext;
    private String mOrderId; //下一版本 开发退票功能所需
    private OrderBizType mBizType;
    /**
     * 退票对话框
     */
    private CancelTicketDialogFragment mCancelTicketDialogFragment;

//    @Inject
//    RefundTicketPresenter mRefundTicketPresenter;

    public PassengerDetailAdapter(Context mContext,
                                  List<Contract> passengerList,
                                  FragmentManager fragmentManager,
                                  String orderId) {
        this.mContext = mContext;
        this.mPassengerList = passengerList;
        this.mFragmentManager = fragmentManager;
        this.mOrderId = orderId;
    }


    public PassengerDetailAdapter(Context mContext,
                                  List<Contract> passengerList,
                                  FragmentManager fragmentManager,
                                  String orderId,
                                  OrderBizType bizType) {
        this.mContext = mContext;
        this.mPassengerList = passengerList;
        this.mFragmentManager = fragmentManager;
        this.mOrderId = orderId;
        this.mBizType = bizType;
    }


    @Override
    public int getCount() {
        return mPassengerList.size();
    }

    @Override
    public Object getItem(int position) {
        return mPassengerList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        DetailViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_history_trip_detail, parent, false);
            viewHolder = new DetailViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (DetailViewHolder) convertView.getTag();
        }

        Contract passenger = mPassengerList.get(position);
        viewHolder.onBindDetailView(passenger, position);

        return convertView;
    }

    public CancelTicketDialogFragment getCancelTicketDialogFragment() {
        return mCancelTicketDialogFragment;
    }

    class DetailViewHolder {
        @BindView(R.id.tv_item_trip_detail_name)
        TextView tvName; //名称
        @BindView(R.id.tv_item_trip_detail_identity)
        TextView tvIdentity;//证件号码
        @BindView(R.id.tv_item_trip_detail_year_old)
        TextView tvYearOld;//成人还是儿童
        @BindView(R.id.tv_item_trip_detail_price)
        TextView tvPrice;//票价
        @BindView(R.id.ll_item_trip_detail_cancel_price)
        LinearLayout mCancelPriceLl;
        @BindView(R.id.iv_item_trip_detail_cancel_price)
        ImageView cancelTicketIv; //退票

        DetailViewHolder(View itemView) {
            ButterKnife.bind(this, itemView);
        }

        @OnClick(R.id.ll_item_trip_detail_cancel_price)
        public void onCancelPriceLayoutClick(LinearLayout linearLayout) {
            //退票
//            if(linearLayout != null){
//                int position = (int)linearLayout.getTag();
//                mCancelTicketDialogFragment = CancelTicketDialogFragment
//                        .newInstance(mOrderId, mPassengers.get(position));
//                mCancelTicketDialogFragment.setPresenter(mRefundTicketPresenter);
//
//                mCancelTicketDialogFragment.show(mFragmentManager, mCancelTicketDialogFragment.getClass().getSimpleName());
//            }

        }

        void onBindDetailView(Contract passenger, int position) {
            if (passenger != null) {
                OrderPayStatus payStatus = passenger.getPayStatus();
                ContractType contractType = passenger.getContractType();

                setRealName(passenger.getRealname());
                setIdentity(passenger.getIdNumber());
                setContactType(mBizType, contractType);
                setPrice(passenger.getPrice());
                setCancelTicket(payStatus, position);

                reactRefundStatus(payStatus);
            }
        }

        void reactRefundStatus(OrderPayStatus payStatus) {
            switch (payStatus) {
                case OPS_REFUND:
                case OPS_PART_REFUND:
                case OPS_REFUNDING:
                    int color = mContext.getResources().getColor(R.color.font_color_gray);
                    tvName.setTextColor(color);
                    tvIdentity.setTextColor(color);
                    tvPrice.setTextColor(color);
                    tvYearOld.setTextColor(color);
                    break;
            }
        }


        void setRealName(String name) {
            if (!TextUtils.isEmpty(name)) {
                tvName.setText(name);
            }
        }

        void setIdentity(String IDCardNumber) {
            String filteredIdentifyNumber = StringUtils.getFilteredIdentifyNumber(IDCardNumber);
            tvIdentity.setText(filteredIdentifyNumber);
        }

        void setContactType(OrderBizType bizType, ContractType contractType) {
            String contractTypeStr;
            switch (contractType) {
                case CHILDREN:
                    contractTypeStr = mContext.getString(R.string.route_detail_children);
                    break;
                case ADULT:
                    contractTypeStr = mContext.getString(R.string.route_detail_adult);
                    break;
                default:
                    contractTypeStr = "";
            }
            if (bizType != null) {
                switch (bizType) {
                    case TICKET:
                    case OBT_DABA_TICKET:
                        contractTypeStr = "";
                        break;
                    default:
                }
            }
            tvYearOld.setText(contractTypeStr);
        }


//        void setPrice(ZMContractType contractType , double price) {
//            if (price >= 0) {
//                double showPrice = 0;
//                switch (contractType){
//                    case ADULT:
//                        showPrice = price;
//                        break;
//                    case CHILDREN:
//                        showPrice = price / 2;
//                        if(bizType != null){
//                            switch (bizType){
//                                case TICKET:
//                                    showPrice = price;
//                                    break;
//                            }
//                        }
//                        break;
//                }
//                tvPrice.setText(String.format("¥%s", StringUtils.priceFormat(showPrice)));
//            }
//        }

        void setPrice(double price) {
            if (price >= 0) {
                tvPrice.setText(String.format("¥%s", StringUtils.priceFormat(price)));
            }
        }

        void setCancelTicket(OrderPayStatus payStatus, int position) {
//            mCancelPriceLl.setTag(position);
//            switch (payStatus) {
//                case PAID:
//                    mCancelPriceLl.setVisibility(View.VISIBLE);
//                    mCancelPriceLl.setClickable(true);
//                    cancelTicketIv.setImageResource(R.drawable.cancel_price_checked);
//                    break;
//                case OPS_REFUNDING:
//                    mCancelPriceLl.setVisibility(View.VISIBLE);
//                    mCancelPriceLl.setClickable(false);
//                    cancelTicketIv.setImageResource(R.drawable.cancel_price_unchecked);
//                    break;
//                default:
//                    mCancelPriceLl.setVisibility(View.INVISIBLE);
//            }
            mCancelPriceLl.setVisibility(View.INVISIBLE);
        }
    }

}
